let img = 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Flmg.jj20.com%2Fup%2Fallimg%2F1114%2F121420113514%2F201214113514-6-1200.jpg&refer=http%3A%2F%2Flmg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1668079578&t=c3b5f77157453918bdc1bcbec70b9a3a';
let vm = new Vue({
    el: '#app',
    data: {
        listArr: [
            { pic: img, name: "秋刀鱼/斤", price: "35.00", dprice: "￥25", num: 1 },
            { pic: img, name: "鲜大带鱼/条", price: "45.00", dprice: "￥35", num: 1 },
            { pic: img, name: "鲜活皮皮虾/斤", price: "85.00", dprice: "￥95", num: 1 },
        ],
    },
    methods: {
        sub(item) {
            if (item.num > 1) {
                item.num--
            }
        },
        add(item) {
            item.num++
        },
        clear() {
            this.listArr = [];

        }
    },
    // 参数：forEach(回调函数)；
    // function(元素数值，[元素下标]，[数组名]) {
    // }
    // 计算总数和总价
    computed:{
        totalNum(){
            let sum = 0;
            this.listArr.forEach((item) => {
                sum += item.num;
            });
            return sum;
        },
        totalPrice() {
            let sum = 0;
            this.listArr.forEach((item) => {
                sum += item.num * item.price;
            });
            return sum;
        },
    }
})